package com.stylefeng.guns.modular.system.warpper;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.stylefeng.guns.modular.system.model.Gdprice;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.stylefeng.guns.core.base.warpper.BaseControllerWarpper;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.model.NbGpcWt;
import com.stylefeng.guns.modular.system.model.Realtime;

public class YdayTimeMonitorWarpper extends BaseControllerWarpper{

	private int wt;
	private Map<String,Boolean> keys;

	public YdayTimeMonitorWarpper(List<Map<String, Object>> list,Map<String,Boolean> keys,Integer wt) {
        super(list);
        this.keys = keys;
        this.wt = wt;
    }

    @Override
    public void warpTheMap(Map<String, Object> map) {
    	DecimalFormat df = new DecimalFormat("0.00");
    	String gScode = (String)map.get("gScode");
		List<Gdprice> prices = ConstantFactory.me().getGdPrices(gScode,DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
		List<Map<String,Object>> jjs = ConstantFactory.me().selectJjStatus(gScode, DateUtil.formatDate((Date)map.get("gTime"), "yyyy-MM-dd"));
		if(CollectionUtils.isEmpty(jjs)){
			map.put("jjs", "无记录");
		}else{
			StringBuffer jjBuffer = new StringBuffer();
        	jjBuffer.append("<b><div style='float: left; width: 70px;'>竞价说明</div>").append("</br>");
        	for(Map<String,Object> v : jjs){
        		jjBuffer.append("<div style='float: left; width: 450px;'><font color='Brown'>" + v.get("gYdDsc") + "</div></font>").append("</br>");
        	}
        	if(CollectionUtils.isEmpty(jjs) || jjs.size() == 1){
        		map.put("ln", "<font color='green'><b>0</b></font>");
        	}else{
        		if(Math.abs((Double)jjs.get(0).get("gBuy")) < 1
        				|| Math.abs((Double)jjs.get(1).get("gBuy")) <1){
        			map.put("ln", "<font color='green'><b>0</b></font>");
        		}else{
        			double ln = Math.abs((Double)jjs.get(0).get("gBuy")) / Math.abs((Double)jjs.get(1).get("gBuy"));
                	if(ln > 1){
                		map.put("ln", "<font color='red'><b>"+df.format(ln)+"</b></font>");
                	}else{
                		map.put("ln", "<font color='green'><b>"+df.format(ln)+"</b></font>");
                	}
        		}
        	}
        	map.put("jjs", jjBuffer.toString());
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append("<b><div style='float: left; width: 95px;'>时间</div><div style='float: left; width: 75px;'>价格</div><div style='float: left; width: 75px;'>涨跌幅</div><div style='float: left; width: 75px;'>总笔数</div>").append("</br>");
		for(Gdprice gdprice : prices){
			buffer.append("<div style='float: left; width: 95px;'>" + DateUtil.formatDate(gdprice.getgTime(), "yyyy-MM-dd") + "</div>")
					.append("<div style='float: left; width: 75px;'>" + gdprice.getgPrice() + "</div>");
			double gZdf = gdprice.getgZdf();
			if(gZdf > 0){
				buffer.append("<div style='float: left; width: 75px;'><font color='red'><b>" + gZdf + "</b></font></div>");
			}else{
				buffer.append("<div style='float: left; width: 75px;'><font color='green'><b>" + gZdf + "</b></font></div>");
			}
			buffer.append("<div style='float: left; width: 75px;'><b>" + gdprice.getgZhs() + "</b></div>").append("</br>");
		}
		map.put("chicang", buffer.toString());
		List<NbGpcWt> realtimes = ConstantFactory.me().selectWtStatus(gScode, DateUtil.formatDate((Date)map.get("gTime"), "yyyy-MM-dd"),wt);
    	map.put("realTimes", realtimes);
    	
		
    	double gZlje = ((Double)map.get("gZlje"));
		if(Math.abs(gZlje) < 100000000){
			map.put("gZlje", (gZlje / 10000) > 0 ? "<font color='red'><b>"+df.format((gZlje / 10000))+"万</b></font>" : "<font color='green'><b>"+df.format((gZlje / 10000))+"万</b></font>");
		}else{
			map.put("gZlje", (gZlje / 100000000) > 0 ? "<font color='red'><b>"+df.format((gZlje / 100000000))+"亿</b></font>" : "<font color='green'><b>"+df.format((gZlje / 100000000))+"亿</b></font>");
		}
		double gZlbuy = ((Double)map.get("gZlbuy"));
		if(Math.abs(gZlbuy) < 100000000){
			map.put("gZlbuy", (gZlbuy / 10000) > 0 ? "<font color='red'><b>"+df.format((gZlbuy / 10000))+"万</b></font>" : "<font color='green'><b>"+df.format((gZlbuy / 10000))+"万</b></font>");
		}else{
			map.put("gZlbuy", (gZlbuy / 100000000) > 0 ? "<font color='red'><b>"+df.format((gZlbuy / 100000000))+"亿</b></font>" : "<font color='green'><b>"+df.format((gZlbuy / 100000000))+"亿</b></font>");
		}
		
		double  gZlshell= ((Double)map.get("gZlsell"));
		if(Math.abs(gZlshell) < 100000000){
			map.put("gZlShell", (gZlshell / 10000) > 0 ? "<font color='red'><b>"+df.format((gZlshell / 10000))+"万</b></font>" : "<font color='green'><b>"+df.format((gZlshell / 10000))+"万</b></font>");
		}else{
			map.put("gZlShell", (gZlshell / 100000000) > 0 ? "<font color='red'><b>"+df.format((gZlshell / 100000000))+"亿</b></font>" : "<font color='green'><b>"+df.format((gZlshell / 100000000))+"亿</b></font>");
		}
		
		double gRate = ((Double)map.get("gZdf"));
		if(gRate > 0){
			map.put("gRate", "<font color='red'><b>"+gRate+"</b></font>");
		}else{
			map.put("gRate", "<font color='green'><b>"+gRate+"</b></font>");
		}
    	
//    	IndustryRank industryRank = ConstantFactory.me().getIndustryRank((String)map.get("gOwerBk"),null);
//    	if(industryRank != null){
//    		map.put("zdf", industryRank.getgZdf() > 0 ? "<font size='2' color='red'><b>"+industryRank.getgZdf()+"</b></font>" : "<font size='2' color='green'><b>"+industryRank.getgZdf()+"</b></font>");
//    	}else{
//    		map.put("zdf", "<font size='2' color='red'><b>0.00</b></font>");
//    	}
    	map.put("gOwerBk","<font color='red'><b>"+map.get("gOwerBk")+"</font></b>");
    	String gJgcydType = ((String)map.get("gJgcydType"));
		String font = "";
		if(gJgcydType.equals("不控盘")){
			font = "<font color='DimGray'><b>不控盘</b></font>";
		}else if(gJgcydType.equals("轻度控盘")){
			font = "<font color='BurlyWood'><b>轻度控盘</b></font>";
		}else if(gJgcydType.equals("中度控盘")){
			font = "<font color='Chocolate'><b>中度控盘</b></font>";
		}else{
			font = "<font color='Brown'><b>完全控盘</b></font>";
		}
		
		double gZs = ((Double)map.get("gZs"));
		if(gZs > 0){
			map.put("gZs", "<font color='red'><b>"+df.format(gZs)+"</b></font>");
		}else{
			map.put("gZs", "<font color='green'><b>"+df.format(gZs)+"</b></font>");
		}
		map.put("gJgcydType", font);
		map.put("gZsz", df.format(Math.abs(((Double)map.get("gSjlt") / 100000000))));
		
		StringBuffer bufferc = new StringBuffer();
		String[] concepts = StringUtils.split((String)map.get("gConceptBk"), "、");
		for(String c : concepts){
			Map<Boolean,Boolean> isMaps = isExits(map,c);
			for(Entry<Boolean, Boolean> e : isMaps.entrySet()){
				if(e.getKey()){
					if(e.getValue()){
						bufferc.append("<font color='green'><b class='blink_green'>"+c+"</font></b>,");
					}else{
						bufferc.append("<font color='Brown'><b class='blink_red'>"+c+"</font></b>,");
					}
				}else{
					bufferc.append("<font color='Chocolate'><b>"+c+"</font></b>,");
				}
			}
		}
		map.put("gConceptBk", bufferc.toString());
		
    }
    
    public static double accuracy(double num, double total){
		double accuracy_num = num / total * 100;
		return accuracy_num;
    }
    
    public static void main(String[] args) {
		System.out.println(accuracy(4055,244249));
	}
    
    private Map<Boolean,Boolean> isExits(Map<String, Object> map,String key){
    	Map<Boolean,Boolean> isMaps = new HashMap<Boolean, Boolean>();
    	boolean isExits = false;
    	for(Entry<String, Boolean> e : keys.entrySet()){
    		if(StringUtils.contains(key, e.getKey())){
				if(e.getValue()){
					isMaps.put(true, true);
					isExits = true;
				}else{
					isMaps.put(true, false);
					isExits = true;
				}
    		}
    	}
    	if(!isExits){
    		isMaps.put(false, false);
    	}
    	return isMaps;
    }
    
    private void putType(Realtime read){
    	if(read.getgType() == 8214l){
    		read.setTemp("<font color='green'><b>60日新低</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8202l){
    		read.setTemp("<font color='red'><b>快速反弹</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8201l){
    		read.setTemp("<font color='red'><b>快速反弹</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8194l){
    		read.setTemp("<font color='green'><b>大笔卖出</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 128l){
    		read.setTemp("<font color='green'><b class='blink_green'>有大卖盘</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8203l){
    		read.setTemp("<font color='green'><b>高台跳水</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8193l){
    		read.setTemp("<font color='red'><b>大笔买入</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8l){
    		read.setTemp("<font color='green'><b>封跌停板</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 32l){
    		read.setTemp("<font color='red'><b>打开跌停板</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 64l){
    		read.setTemp("<font color='red'><b class='blink_red'>有大买盘</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 4l){
    		read.setTemp("<font color='red'><b>封涨停板</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8215l){
    		read.setTemp("<font color='red'><b>60日大幅上涨</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 16l){
    		read.setTemp("<font color='green'><b>打开涨停板</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8213l){
    		read.setTemp("<font color='red'><b>60日新高</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8204l){
    		read.setTemp("<font color='green'><b>加速下跌</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8216l){
    		read.setTemp("<font color='green'><b>60日大幅下跌</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8212l){
    		read.setTemp("<font color='green'><b>向下缺口</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8211l){
    		read.setTemp("<font color='red'><b>向上缺口</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8207l){
    		read.setTemp("<font color='red'><b>竞价上涨</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8208l){
    		read.setTemp("<font color='green'><b>竞价下跌</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8209l){
    		read.setTemp("<font color='red'><b>高开5日线</font></b>");
    		read.setgDec("<font color='red'><b>"+read.getgDec()+"</font></b>");
    	}else if(read.getgType() == 8210l){
    		read.setTemp("<font color='green'><b>低开5日线</font></b>");
    		read.setgDec("<font color='green'><b>"+read.getgDec()+"</font></b>");
    	}
    }
}
